package com.UnionPayCenter.controller;

import com.UnionPayCenter.entity.ManagerEntity;
import com.UnionPayCenter.entity.ResponseWrapper;
import com.UnionPayCenter.entity.vo.ManagerVo;
import com.UnionPayCenter.enums.ErrorCodeEnum;
import com.UnionPayCenter.exception.ApiException;
import com.UnionPayCenter.jwt.CurrentManager;
import com.UnionPayCenter.service.IManagerService;
import com.UnionPayCenter.sms.StringUtil;
import com.UnionPayCenter.swagger.ApiJsonObject;
import com.UnionPayCenter.swagger.ApiJsonProperty;
import com.UnionPayCenter.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: UnionPayCenter
 * @description: 管理员
 * @author: bite
 * @create:
 **/
@Slf4j
@Api(value = "manager", tags = "管理员")
@RestController
@RequestMapping("/manager")
public class ManagerController {
    @Autowired
    private IManagerService managerService;

    @RequestMapping(value = "/checkMobile", method = RequestMethod.POST)
    @ApiOperation(tags = "管理员", httpMethod = "POST", value = "[后台]验证手机号是否存在", response = boolean.class)
    public ResponseWrapper checkMobile(
            @ApiJsonObject(name = "managerCheckMobileParams", value = {
                    @ApiJsonProperty(key = "mobile", value = "手机号")
            })@RequestBody JSONObject jsonParam) {
        try {
            String mobile = jsonParam.getString("mobile");
            if (StringUtils.isNullStr(mobile)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "手机号码"));
            }
            //true：可用 false：不可用
            boolean isExist = managerService.chenMobile(jsonParam);
            return ResponseWrapper.succeed(isExist);
        }catch (ApiException e){
            log.error(String.format("[后台]验证手机号是否存在失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]验证手机号是否存在失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @RequestMapping(value = "/getVerificationCode", method = RequestMethod.POST)
    @ApiOperation(tags = "管理员", httpMethod = "POST", value = "[后台]商家注册获取验证码", response = String.class)
    public ResponseWrapper getVerificationCode(
            @ApiJsonObject(name = "managerGetVerificationCodeParams", value = {
                    @ApiJsonProperty(key = "mobile", value = "手机号"),
                    @ApiJsonProperty(key = "type", value = "0-注册 1-忘记密码", type = "int")
            }) @RequestBody JSONObject jsonParam) {
        try {
            String mobile = jsonParam.getString("mobile");
            if (StringUtils.isNullStr(mobile)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "手机号码"));
            }
            //0-注册 1-忘记密码
            Integer type = jsonParam.getInteger("type");
            if (type == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "类型"));
            }
            managerService.getVerificationCode(jsonParam);
            return ResponseWrapper.succeed("发送成功");
        }catch (ApiException e){
            log.error(String.format("[后台]商家注册获取验证码失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]商家注册获取验证码失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @RequestMapping(value = "/registry", method = RequestMethod.POST)
    @ApiOperation(tags = "管理员", httpMethod = "POST", value = "[后台]商家注册", response = String.class)
    public ResponseWrapper registry(
            @ApiJsonObject(name = "managerRegistryParams", value = {
                    @ApiJsonProperty(key = "mobile", value = "手机号"),
                    @ApiJsonProperty(key = "code", value = "验证码"),
                    @ApiJsonProperty(key = "password", value = "密码"),
                    @ApiJsonProperty(key = "repeatPassword", value = "再次密码")
            }) @RequestBody JSONObject jsonParam) {
        try {
            String mobile = jsonParam.getString("mobile");
            if (StringUtils.isNullStr(mobile)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "手机号"));
            }
            String code = jsonParam.getString("code");
            if (StringUtils.isNullStr(code)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "验证码"));
            }
            String password = jsonParam.getString("password");
            if (StringUtils.isNullStr(password)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "密码"));
            }
            String repeatPassword = jsonParam.getString("repeatPassword");
            if (StringUtils.isNullStr(repeatPassword)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "再次密码"));
            }
            if(!password.equals(repeatPassword)){
                return ResponseWrapper.failed(ErrorCodeEnum.SW40);
            }
            managerService.registry(jsonParam);
            return ResponseWrapper.succeed("注册成功");
        }catch (ApiException e){
            log.error(String.format("[后台]商家注册失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]商家注册失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @RequestMapping(value = "/forgetPassword", method = RequestMethod.POST)
    @ApiOperation(tags = "管理员", httpMethod = "POST", value = "[后台]找回密码", response = String.class)
    public ResponseWrapper forgetPassword(
            @ApiJsonObject(name = "managerForgetPasswordParams", value = {
                    @ApiJsonProperty(key = "mobile", value = "手机号"),
                    @ApiJsonProperty(key = "code", value = "验证码"),
                    @ApiJsonProperty(key = "password", value = "密码"),
                    @ApiJsonProperty(key = "repeatPassword", value = "再次密码")
            }) @RequestBody JSONObject jsonParam) {
        try {
            String mobile = jsonParam.getString("mobile");
            if (StringUtils.isNullStr(mobile)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "手机号"));
            }
            String code = jsonParam.getString("code");
            if (StringUtils.isNullStr(code)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "验证码"));
            }
            String password = jsonParam.getString("password");
            if (StringUtils.isNullStr(password)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "密码"));
            }
            String repeatPassword = jsonParam.getString("repeatPassword");
            if (StringUtils.isNullStr(repeatPassword)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "再次密码"));
            }
            if(!password.equals(repeatPassword)){
                return ResponseWrapper.failed(ErrorCodeEnum.SW40);
            }
            managerService.forgetPassword(jsonParam);
            return ResponseWrapper.succeed("密码修改成功");
        }catch (ApiException e){
            log.error(String.format("[后台]找回密码失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]找回密码失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation(tags = "管理员", httpMethod = "POST", value = "[后台]管理员登录", response = ManagerVo.class)
    public ResponseWrapper login(
            @ApiJsonObject(name = "managerLoginParams", value = {
                    @ApiJsonProperty(key = "username", value = "账户名"),
                    @ApiJsonProperty(key = "password", value = "密码")
            })@RequestBody JSONObject jsonParam) {
        try {
            String username = jsonParam.getString("username");
            String password = jsonParam.getString("password");
            if (StringUtils.isNullStr(username) || StringUtils.isNullStr(password)) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW20);
            }

            ManagerVo managerVo = managerService.login(jsonParam);
            return ResponseWrapper.succeed(managerVo);
        }catch (ApiException e){
            log.error(String.format("[后台]管理员登录失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]管理员登录失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @CurrentManager
    @RequestMapping(value = "/loginOut", method = RequestMethod.POST)
    @ApiOperation(tags = "管理员", httpMethod = "POST", value = "[后台]注销登录", response = String.class)
    public ResponseWrapper loginOut(
            @ApiJsonObject(name = "managerLoginOutParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值")
            })@RequestBody JSONObject jsonParam) {
        try {
            managerService.loginOut(jsonParam);
            return ResponseWrapper.succeed("注销成功");
        }catch (ApiException e){
            log.error(String.format("[后台]注销登录失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]注销登录失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }

    @CurrentManager
    @RequestMapping(value = "/getInfo", method = RequestMethod.POST)
    @ApiOperation(tags = "管理员", httpMethod = "POST", value = "[后台]获取管理员信息", response = ManagerEntity.class)
    public ResponseWrapper getInfo(
            @ApiJsonObject(name = "managerGetInfoParams", value = {
                    @ApiJsonProperty(key = "token", value = "token值")
            }) @RequestBody JSONObject jsonParam) {
        try {
            ManagerEntity managerEntity = managerService.getInfo(jsonParam);
            return ResponseWrapper.succeed(managerEntity);
        }catch (ApiException e){
            log.error(String.format("[后台]获取管理员信息失败，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[后台]获取管理员信息失败"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
    
    @RequestMapping(value = "/saveCardNum", method = RequestMethod.POST)
    @ApiOperation(tags = "管理员", httpMethod = "POST", value = "[前端]保存会员卡号", response = ManagerEntity.class)
    public ResponseWrapper saveCardNum(
            @ApiJsonObject(name = "managerSaveCardNumParams", value = {
                    @ApiJsonProperty(key = "mobile", value = "手机号"),
                    @ApiJsonProperty(key = "code", value = "code")
            }) @RequestBody JSONObject jsonParam) {
        try {
            String mobile = jsonParam.getString("mobile");
            String code = jsonParam.getString("code");
            if (mobile == null) {
                return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "手机号"));
            }
            if (code == null) {
            	return ResponseWrapper.failed(ErrorCodeEnum.SW11.code(), String.format(ErrorCodeEnum.SW11.msg(), "code"));
            }

            ManagerEntity manager = managerService.saveCardNum(jsonParam);
            return ResponseWrapper.succeed(manager);
        }catch (ApiException e){
            log.error(String.format("[前端]保存会员卡号，参数:%s, 错误信息:%s", jsonParam.toJSONString(), e.getMessage()));
            return ResponseWrapper.failed(e.getCode(), e.getMessage());
        }catch (Exception e){
            log.error(String.format("[前端]保存会员卡号"), e);
            return ResponseWrapper.failed(ErrorCodeEnum.FL500);
        }
    }
}
